package com.xesapp.uiautomator;

import java.io.IOException;

import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.core.UiWatcher;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class UiautomatorMain extends UiAutomatorTestCase {

    public void testDemo() throws Exception {
        System.out.println("渠道包验证！！！！！！！！！！！！！！！");
        UiDevice.getInstance().registerWatcher("SystemAlert", new UiWatcher() {
            @Override
            public boolean checkForCondition() {
                // 监听系统弹框
                UiObject call = new UiObject(
                        new UiSelector()
                                .resourceId("com.android.packageinstaller:id/permission_allow_button"));
                UiObject call_homePage = new UiObject(
                        new UiSelector()
                                .resourceId("com.xes.jazhanghui.activity:id/iv_home_activity_close"));

                if (call.exists()) {
                    System.out.println("监听到系统弹框，点击关闭");
                    try {
                        call.click();
                        return true;
                    } catch (UiObjectNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                if (call_homePage.exists()) {
                    System.out.println("监听到广告弹框，点击关闭");
                    try {
                        call_homePage.click();
                        return true;
                    } catch (UiObjectNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                return false;
            }
        });

        findAndRunApp();
        // 点击同意
        click_Tongyi();
        // 点击"已满14岁"
        click_Guard();
        // 点击首页购物车
        click_ShopCart();
        // 点击登陆权限弹框（电话状态）
        click_dialog();
        // 切换账号密码
        click_loginChange();
        // 输出账号
        click_sendLoginName();
        // 输出密码
        click_sendPasswork();
        // 点击同意协议
        click_privacyCheck();
        // 点击登陆按钮
        click_loginButton();
        // 点击位置权限
        click_positionButton();
        // 点击首页课程包
        click_curriculum();
        // 点击课程包中的班级
        click_class();
    }

    // 启动学而思app
    public void findAndRunApp() throws UiObjectNotFoundException {
        try {
            Runtime.getRuntime()
                    .exec("am start -n com.xes.jazhanghui.activity/.mvp.start.StartActivity");
        } catch (IOException e) {
            e.printStackTrace();
        }
        sleep(3000);
    }

    public void click_Tongyi() throws UiObjectNotFoundException {
        UiObject lb = new UiObject(
                new UiSelector()
                        .resourceId("com.xes.jazhanghui.activity:id/btn_sure"));
        System.out.println("点击同意点击同意");
        lb.click();
        sleep(3000);
    }

    public void click_Guard() throws UiObjectNotFoundException {

        UiObject qx = new UiObject(new UiSelector().text("去开启"));
        if (qx.exists()) {
            System.out.println("位置读取：去开启存在");
            qx.click();
            sleep(3000);
        } else {
            System.out.println("去开启不存在");
        }

        UiObject aq = new UiObject(new UiSelector().text("去开启"));
        if (aq.exists()) {
            System.out.println("电话读取：去开启存在");
            aq.click();
            sleep(3000);
        } else {
            System.out.println("去开启不存在");
        }

        UiObject lb = new UiObject(
                new UiSelector()
                        .resourceId("com.xes.jazhanghui.activity:id/btn_commondialog_pos"));
        System.out.println("点击已满14岁");
        lb.click();
        sleep(3000);
    }

    public void click_ShopCart() throws UiObjectNotFoundException {
        UiObject lb = new UiObject(
                new UiSelector()
                        .resourceId("com.xes.jazhanghui.activity:id/btn_commondialog_pos"));
        if (lb.exists()) {
            System.out.println("已满14岁存在，点掉");
            lb.click();
            sleep(3000);
        } else {
            System.out.println("已满14岁不存在");
        }

        // 获得屏幕宽高
        UiDevice device = getUiDevice();
        int height = device.getDisplayHeight();
        int width = device.getDisplayWidth();
        System.out.println("购物车");
        device.click(width - 100, 120);
        sleep(3000);// 干等2分钟

        // UiObject qx = new UiObject(new UiSelector().text("后台下载"));
        // if (qx.exists()) {
        // System.out.println("后台下载存在");
        // //qx.click();
        // sleep(3000);
        // } else {
        // System.out.println("去开启不存在");
        // // something
        // }
        // 严重大坑 资源下载时间问题（严重但是不深）
    }

    public void click_dialog() throws UiObjectNotFoundException {
        UiObject lb = new UiObject(
                new UiSelector()
                        .resourceId("com.xes.jazhanghui.activity:id/btn_commondialog_pos"));
        System.out.println("点击端内权限弹框");
        lb.click();
        sleep(3000);
    }

    public void click_privacyCheck() throws UiObjectNotFoundException {
        try {
            Runtime.getRuntime().exec("monkey -f /data/local/tmp/1.txt 1");
        } catch (IOException e) {
            e.printStackTrace();
        }
        sleep(3000);
    }

    public void click_loginChange() throws UiObjectNotFoundException {
        UiObject login = new UiObject(new UiSelector().text("密码登录"));
        System.out.println("点击切换密码登陆");
        if (!login.exists()) {
            login.click();
        } else {
        }
        sleep(3000);
    }

    public void click_sendLoginName() throws UiObjectNotFoundException {
        UiObject lb = new UiObject(
                new UiSelector()
                        .resourceId("com.xes.jazhanghui.activity:id/xes_login_username"));
        System.out.println("输入账号");
        lb.setText("17788880041");
        sleep(3000);
    }

    public void click_sendPasswork() throws UiObjectNotFoundException {
        UiObject lb = new UiObject(
                new UiSelector()
                        .resourceId("com.xes.jazhanghui.activity:id/xes_login_password"));
        System.out.println("输入密码");
        lb.setText("111111");
        sleep(3000);
    }

    public void click_loginButton() throws UiObjectNotFoundException {
        UiObject lb = new UiObject(
                new UiSelector()
                        .resourceId("com.xes.jazhanghui.activity:id/xes_login_button"));
        System.out.println("点击登陆");
        lb.click();
        sleep(6000);
    }

    public void click_positionButton() throws UiObjectNotFoundException {
        UiObject lb = new UiObject(
                new UiSelector()
                        .resourceId("com.xes.jazhanghui.activity:id/btn_commondialog_pos"));
        System.out.println("开启位置权限");
        lb.click();
        sleep(3000);
    }

    public void click_curriculum() throws UiObjectNotFoundException {
        UiObject lb = new UiObject(
                new UiSelector()
                        .resourceId("com.xes.jazhanghui.activity:id/cardRoot"));
        System.out.println("点击课程包");
        lb.click();
        sleep(3000);
    }

    public void click_class() throws UiObjectNotFoundException {
        UiObject lb = new UiObject(
                new UiSelector()
                        .resourceId("com.xes.jazhanghui.activity:id/courseCardView"));
        System.out.println("点击班级卡片");
        lb.click();
        sleep(3000);
    }

}
